package com.lucan.tracking.compiler;

import com.lucan.tracking.utils.Logger;
import com.squareup.javapoet.FieldSpec;

import java.lang.reflect.Type;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.lang.model.element.Modifier;
import javax.lang.model.util.Elements;

public abstract class BaseProcessor extends AbstractProcessor {
    private Logger mLogger;
    private Filer mFiler;
    private Elements mElementUtils;

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> typeSet = new LinkedHashSet<>();
        for (Class claz : getSupportAnnotations()) {
            typeSet.add(claz.getCanonicalName());
        }
        return typeSet;
    }

    public abstract Class[] getSupportAnnotations();

    protected Logger getLogger() {
        if (mLogger == null) {
            mLogger = new Logger(processingEnv.getMessager());
        }
        return mLogger;
    }

    protected Filer getFiler() {
        if (mFiler == null) {
            mFiler = processingEnv.getFiler();
        }
        return mFiler;
    }

    protected Elements getUtils() {
        if (mElementUtils == null) {
            mElementUtils = processingEnv.getElementUtils();
        }
        return mElementUtils;
    }

    /**
     * @param fieldName
     * @return
     */
    protected FieldSpec buildStaticConfigField(String fieldName, Type type, String format, Object... args) {
        return FieldSpec.builder(type, fieldName)
                .addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC)
                .initializer(format, args)
                .build();
    }
}
